package com.foruo.code.main;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class Temp {
    public JPanel TempMain;
    private JTextArea statusLog;//状态显示
    private JTree dbTree;//数据库树
    private JTextField projectPath;//项目路径
    private TreePath[] treePath = null;//节点路径

    public Temp() {
        //初始组件
        initComponents();
        initBindComponents();
    }

    /**
     * 初始化组件
     */
    private void initComponents() {
//        initValue();
        //加载数据树
        loadTree();
    }

    /**
     * 初始化绑定组件
     */
    private void initBindComponents() {
        //树点击
        dbTree.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent evt) {
//                //选择数据库
//                if (evt.isMetaDown()) {
//                    //右键 没有子节点
                dbTree = (JTree) evt.getSource();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) dbTree
                        .getLastSelectedPathComponent();
                if (node != null && node.getChildCount() == 0) {
                    //最终子节点
                    treePath = dbTree.getSelectionPaths();
                }
//                }
                TreePath tmp = treePath[0];
//                JOptionPane.showMessageDialog(new JPanel(), "提示消息："+tmp.getPath()[1].toString(), "标题",JOptionPane.WARNING_MESSAGE);
                String name = tmp.getPath()[1].toString();
                String content = getFileContent(new File("template" + File.separator + name));
                if (content != null) {
//                    statusLog.removeAll();
                    statusLog.setText(content + "\r\n");
//                    statusLog.append(content + "\r\n");
                    statusLog.setCaretPosition(0);
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }


    /**
     * 更新数据库树
     */
    public void loadTree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("模板列表");
        DefaultTreeModel treeMode = new DefaultTreeModel(root);
        List<String> list = getFileList("template");
        for (int j = 0; j < list.size(); j++) {
//            DefaultMutableTreeNode leftnode1 = new DefaultMutableTreeNode(list.get(j) + "(模板)");
            DefaultMutableTreeNode leftnode1 = new DefaultMutableTreeNode(list.get(j));
            root.add(leftnode1);
        }
        dbTree.setModel(treeMode);
    }

    public static List<String> getFileList(String path) {
//        String path = "template"; // 路径
        List<String> list = new ArrayList<String>();
        File f = new File(path);
        if (!f.exists()) {
            System.out.println(path + " not exists");
            return null;
        }
        File fa[] = f.listFiles();
        for (int i = 0; i < fa.length; i++) {
            File fs = fa[i];
            if (fs.isDirectory()) {
                System.out.println(fs.getName() + " [目录]");
            } else {
                list.add(fs.getName());
                System.out.println(fs.getName());
            }
        }
        return list;
    }

    public static String getFileContent(File file) {
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        TempMain = new JPanel();
        TempMain.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        TempMain.setBorder(BorderFactory.createTitledBorder("模板"));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return TempMain;
    }
}
